//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CmsLeg.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/Indexes/SwapIndex.h>
#include <gen/QL/CashFlow.h>
#include <gen/QL/Times/DayCounter.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CCmsLeg::CCmsLeg (Cephei::QL::Times::ISchedule^ schedule, Cephei::QL::Indexes::ISwapIndex^ swapIndex) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CSchedule^ _Cschedule;
    CSwapIndex^ _CswapIndex;
    try
    {
#ifdef HANDLE
        _phCmsLeg = NULL;
#endif
        _Cschedule = safe_cast<CSchedule^> (schedule);
        _Cschedule->Lock();
        QuantLib::Schedule& _schedule = static_cast<QuantLib::Schedule&> (_Cschedule->GetReference ()); 
        _CswapIndex = safe_cast<CSwapIndex^> (swapIndex);
        _CswapIndex->Lock();
        boost::shared_ptr<QuantLib::SwapIndex>& _swapIndex = static_cast<boost::shared_ptr<QuantLib::SwapIndex>&> (_CswapIndex->GetShared ()); 
        _ppCmsLeg = new boost::shared_ptr<QuantLib::CmsLeg> (new QuantLib::CmsLeg ( _schedule,  _swapIndex ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cschedule != nullptr) _Cschedule->Unlock();
        if (_CswapIndex != nullptr) _CswapIndex->Unlock();
    }
}
Cephei::QL::Cashflows::CCmsLeg::CCmsLeg (boost::shared_ptr<QuantLib::CmsLeg>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCmsLeg = NULL;
#endif
	_ppCmsLeg = &childNative;
    
}
Cephei::QL::Cashflows::CCmsLeg::CCmsLeg (QuantLib::CmsLeg& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCmsLeg = NULL;
#endif
	_ppCmsLeg = new boost::shared_ptr<QuantLib::CmsLeg> (&childNative);
    
    _CmsLegOwner = owner;
    
}

Cephei::QL::Cashflows::CCmsLeg::CCmsLeg (CCmsLeg^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCmsLeg = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCmsLeg = new boost::shared_ptr<QuantLib::CmsLeg> (copy->GetShared());
        
    }
}
Cephei::QL::Cashflows::CCmsLeg::CCmsLeg (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCmsLeg = NULL;
#endif
	if (!t->IsSubclassOf(CCmsLeg::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CCmsLeg::CCmsLeg (QuantLib::Handle<QuantLib::CmsLeg>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCmsLeg = &childNative;
	_ppCmsLeg = &static_cast<boost::shared_ptr<QuantLib::CmsLeg>>(childNative.currentLink());
    
    _CmsLegOwner = owner;
}
Cephei::QL::Cashflows::CCmsLeg::CCmsLeg (QuantLib::Handle<QuantLib::CmsLeg> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCmsLeg = &childNative;
	_ppCmsLeg = &static_cast<boost::shared_ptr<QuantLib::CmsLeg>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CCmsLeg::CCmsLeg (QuantLib::CmsLeg childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCmsLeg = NULL;
#endif
	_ppCmsLeg = new boost::shared_ptr<QuantLib::CmsLeg> (new QuantLib::CmsLeg (childNative));
    
}
#endif

Cephei::QL::Cashflows::CCmsLeg::~CCmsLeg ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCmsLeg != NULL)
    {
	    delete _ppCmsLeg;
        _ppCmsLeg = NULL;
    }
}
Cephei::QL::Cashflows::CCmsLeg::!CCmsLeg ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCmsLeg != NULL)
    {
	    delete _ppCmsLeg;
    }
}
QuantLib::CmsLeg& Cephei::QL::Cashflows::CCmsLeg::GetReference ()
{
    if (_ppCmsLeg == NULL) throw gcnew NativeNullException ();
	return **_ppCmsLeg;
}
boost::shared_ptr<QuantLib::CmsLeg>& Cephei::QL::Cashflows::CCmsLeg::GetShared ()
{
    if (_ppCmsLeg == NULL) throw gcnew NativeNullException ();
	return *_ppCmsLeg;
}
QuantLib::CmsLeg* Cephei::QL::Cashflows::CCmsLeg::GetPointer ()
{
    if (_ppCmsLeg == NULL) throw gcnew NativeNullException ();
	return &**_ppCmsLeg;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CmsLeg>& Cephei::QL::Cashflows::CCmsLeg::GetHandle ()
{
	if (_phCmsLeg == NULL)
	{
		_phCmsLeg = new Handle<QuantLib::CmsLeg> (*_ppCmsLeg);
	}
	return *_phCmsLeg;
}
#endif
bool Cephei::QL::Cashflows::CCmsLeg::HasNative () 
{
	return (_ppCmsLeg != NULL);
}

Cephei::QL::Cashflows::ICmsLeg^ Cephei::QL::Cashflows::CCmsLeg::InArrears (Microsoft::FSharp::Core::FSharpOption<Boolean>^ flag)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        bool _flag = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (flag) ? (bool)ValueHelper::Convert (flag->Value) : true); //9a
    	QuantLib::CmsLeg& _rv = (QuantLib::CmsLeg&)(*_ppCmsLeg)->inArrears ( _flag );   
        Cephei::QL::Cashflows::CCmsLeg^ _nrv = gcnew Cephei::QL::Cashflows::CCmsLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::ICmsLeg^ Cephei::QL::Cashflows::CCmsLeg::WithCaps (Cephei::IVector<Double>^ caps)
{
    CoVector<Double>^ _Ccaps;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ccaps = safe_cast<CoVector<Double>^> (caps);
        _Ccaps->Lock();
        INativeVector<Double>^ _NCIcaps = _Ccaps->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCcaps = safe_cast<CDoubleVector^>(_NCIcaps);
        std::vector<QuantLib::Rate>& _caps = static_cast<std::vector<QuantLib::Rate>&> (_NCcaps->GetReference ());
    	QuantLib::CmsLeg& _rv = (QuantLib::CmsLeg&)(*_ppCmsLeg)->withCaps ( _caps );   
        Cephei::QL::Cashflows::CCmsLeg^ _nrv = gcnew Cephei::QL::Cashflows::CCmsLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccaps != nullptr) _Ccaps->Unlock();
    }
}
Cephei::QL::Cashflows::ICmsLeg^ Cephei::QL::Cashflows::CCmsLeg::WithFixingDays (Cephei::IVector<UInt32>^ fixingDays)
{
    CoVector<UInt32>^ _CfixingDays;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CfixingDays = safe_cast<CoVector<UInt32>^> (fixingDays);
        _CfixingDays->Lock();
        INativeVector<UInt32>^ _NCIfixingDays = _CfixingDays->getFeature (NativeFeature::Value);
        CUInt32Vector^ _NCfixingDays = safe_cast<CUInt32Vector^>(_NCIfixingDays);
        std::vector<QuantLib::Natural>& _fixingDays = static_cast<std::vector<QuantLib::Natural>&> (_NCfixingDays->GetReference ());
    	QuantLib::CmsLeg& _rv = (QuantLib::CmsLeg&)(*_ppCmsLeg)->withFixingDays ( _fixingDays );   
        Cephei::QL::Cashflows::CCmsLeg^ _nrv = gcnew Cephei::QL::Cashflows::CCmsLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CfixingDays != nullptr) _CfixingDays->Unlock();
    }
}
Cephei::QL::Cashflows::ICmsLeg^ Cephei::QL::Cashflows::CCmsLeg::WithFloors (Cephei::IVector<Double>^ floors)
{
    CoVector<Double>^ _Cfloors;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cfloors = safe_cast<CoVector<Double>^> (floors);
        _Cfloors->Lock();
        INativeVector<Double>^ _NCIfloors = _Cfloors->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfloors = safe_cast<CDoubleVector^>(_NCIfloors);
        std::vector<QuantLib::Rate>& _floors = static_cast<std::vector<QuantLib::Rate>&> (_NCfloors->GetReference ());
    	QuantLib::CmsLeg& _rv = (QuantLib::CmsLeg&)(*_ppCmsLeg)->withFloors ( _floors );   
        Cephei::QL::Cashflows::CCmsLeg^ _nrv = gcnew Cephei::QL::Cashflows::CCmsLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cfloors != nullptr) _Cfloors->Unlock();
    }
}
Cephei::QL::Cashflows::ICmsLeg^ Cephei::QL::Cashflows::CCmsLeg::WithGearings (Cephei::IVector<Double>^ gearings)
{
    CoVector<Double>^ _Cgearings;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cgearings = safe_cast<CoVector<Double>^> (gearings);
        _Cgearings->Lock();
        INativeVector<Double>^ _NCIgearings = _Cgearings->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCgearings = safe_cast<CDoubleVector^>(_NCIgearings);
        std::vector<QuantLib::Real>& _gearings = static_cast<std::vector<QuantLib::Real>&> (_NCgearings->GetReference ());
    	QuantLib::CmsLeg& _rv = (QuantLib::CmsLeg&)(*_ppCmsLeg)->withGearings ( _gearings );   
        Cephei::QL::Cashflows::CCmsLeg^ _nrv = gcnew Cephei::QL::Cashflows::CCmsLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cgearings != nullptr) _Cgearings->Unlock();
    }
}
Cephei::QL::Cashflows::ICmsLeg^ Cephei::QL::Cashflows::CCmsLeg::WithNotionals (Cephei::IVector<Double>^ notionals)
{
    CoVector<Double>^ _Cnotionals;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cnotionals = safe_cast<CoVector<Double>^> (notionals);
        _Cnotionals->Lock();
        INativeVector<Double>^ _NCInotionals = _Cnotionals->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCnotionals = safe_cast<CDoubleVector^>(_NCInotionals);
        std::vector<QuantLib::Real>& _notionals = static_cast<std::vector<QuantLib::Real>&> (_NCnotionals->GetReference ());
    	QuantLib::CmsLeg& _rv = (QuantLib::CmsLeg&)(*_ppCmsLeg)->withNotionals ( _notionals );   
        Cephei::QL::Cashflows::CCmsLeg^ _nrv = gcnew Cephei::QL::Cashflows::CCmsLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cnotionals != nullptr) _Cnotionals->Unlock();
    }
}
Cephei::QL::Cashflows::ICmsLeg^ Cephei::QL::Cashflows::CCmsLeg::WithPaymentAdjustment (QL::Times::BusinessDayConventionEnum convention)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::BusinessDayConvention _convention = (QuantLib::BusinessDayConvention)convention ;
    	QuantLib::CmsLeg& _rv = (QuantLib::CmsLeg&)(*_ppCmsLeg)->withPaymentAdjustment ( _convention );   
        Cephei::QL::Cashflows::CCmsLeg^ _nrv = gcnew Cephei::QL::Cashflows::CCmsLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::ICmsLeg^ Cephei::QL::Cashflows::CCmsLeg::WithPaymentDayCounter (Cephei::QL::Times::IDayCounter^ dayCounter)
{
    CDayCounter^ _CdayCounter;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
    	QuantLib::CmsLeg& _rv = (QuantLib::CmsLeg&)(*_ppCmsLeg)->withPaymentDayCounter ( _dayCounter );   
        Cephei::QL::Cashflows::CCmsLeg^ _nrv = gcnew Cephei::QL::Cashflows::CCmsLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Cashflows::ICmsLeg^ Cephei::QL::Cashflows::CCmsLeg::WithSpreads (Cephei::IVector<Double>^ spreads)
{
    CoVector<Double>^ _Cspreads;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cspreads = safe_cast<CoVector<Double>^> (spreads);
        _Cspreads->Lock();
        INativeVector<Double>^ _NCIspreads = _Cspreads->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCspreads = safe_cast<CDoubleVector^>(_NCIspreads);
        std::vector<QuantLib::Spread>& _spreads = static_cast<std::vector<QuantLib::Spread>&> (_NCspreads->GetReference ());
    	QuantLib::CmsLeg& _rv = (QuantLib::CmsLeg&)(*_ppCmsLeg)->withSpreads ( _spreads );   
        Cephei::QL::Cashflows::CCmsLeg^ _nrv = gcnew Cephei::QL::Cashflows::CCmsLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cspreads != nullptr) _Cspreads->Unlock();
    }
}
Cephei::QL::Cashflows::ICmsLeg^ Cephei::QL::Cashflows::CCmsLeg::WithZeroPayments (Microsoft::FSharp::Core::FSharpOption<Boolean>^ flag)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        bool _flag = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (flag) ? (bool)ValueHelper::Convert (flag->Value) : true); //9a
    	QuantLib::CmsLeg& _rv = (QuantLib::CmsLeg&)(*_ppCmsLeg)->withZeroPayments ( _flag );   
        Cephei::QL::Cashflows::CCmsLeg^ _nrv = gcnew Cephei::QL::Cashflows::CCmsLeg (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::ICmsLeg^ Cephei::QL::Cashflows::CCmsLeg_Factory::Create (Cephei::QL::Times::ISchedule^ schedule, Cephei::QL::Indexes::ISwapIndex^ swapIndex)
{
    return gcnew CCmsLeg ( schedule,  swapIndex);
}
